package com.fmsh.nfcsensor.tools;

import android.nfc.Tag;
import android.nfc.tech.NfcA;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;

import com.fmsh.base.handler.BaseHandler;
import com.fmsh.base.utils.FMUtil;
import com.fmsh.base.utils.LogUtil;
import com.fmsh.nfcsensor.R;
import com.fmsh.nfcsensor.bean.GasIndexAlgorithmParams;
import com.fmsh.nfcsensor.bean.SensorBean;
import com.fmsh.nfcsensor.utils.Constants;
import com.fmsh.nfcsensor.utils.SensorUtils;

import java.io.IOException;
import java.util.ArrayList;


/**
 * @author wuyajiang
 * @date 2022/2/28
 */
public class NFCUtils {

    public static void startNfcA(Bundle bundle) {
        Tag tag = bundle.getParcelable(Constants.KEY_NFC_TAG);
        NfcA nfcA = NfcA.get(tag);
        if (nfcA == null) {
            sendMsg(Constants.NFC_NO_TYPE_NFCA);
        } else {
            try {
                if (!nfcA.isConnected()) {
                    nfcA.connect();
                }
                if (!init(nfcA)) {
                    return;
                }
                int position = bundle.getInt(Constants.KEY_NFCA_OPERATION_TYPE);
                switch (position) {
                    case 0:
                        handlerSensor(nfcA);
                        break;
                    case 1:
                        startSensor(nfcA, bundle.getInt(Constants.KEY_NFCA_OPERATION_TYPE + 1));
                        break;
                    case 2:
                        break;


                }

            } catch (Exception e) {
                e.printStackTrace();
                sendMsg(Constants.NFC_UNKNOWN_ERROR);
            } finally {
                //                try {
                //                    nfcA.close();
                //                } catch (IOException e) {
                //                    e.printStackTrace();
                //                }
            }

        }
    }

    public static boolean init(NfcA nfcA) throws IOException {
        byte[] auth = nfcA.transceive(FMUtil.hexToByte(Constants.COMMAND_AUTH));
        LogUtil.d(FMUtil.byteToHex(auth));
        if (!"0000".equals(FMUtil.byteToHex(auth))) {
            sendMsg(Constants.NFC_AUTH_FAILED);
            return false;
        }

        return true;
    }

    public static void handlerSensor(NfcA nfcA) throws IOException {
        byte[] reg = nfcA.transceive(FMUtil.hexToByte(Constants.COMMAND_WRITE_REG));
        LogUtil.d(FMUtil.byteToHex(reg));
        if (!"AAAA".equals(FMUtil.byteToHex(reg))) {
            sendMsg(Constants.NFC_UNKNOWN_ERROR);
            return;
        }
        byte[] sensor = nfcA.transceive(FMUtil.hexToByte(Constants.COMMAND_READ_SENSOR));
        LogUtil.d(FMUtil.byteToHex(sensor));


        SensorBean tSensor = null;
        SensorBean hSensor = null;
        SensorBean pSensor = null;
        SensorBean bSensor = null;
        SensorBean gSensor = null;
        SensorBean cSensor = null;
        byte[] dev = null;
        for (int i = 0; i < sensor[0]; i++) {
            int type = (sensor[i + 1] & (byte) 0xF0) >> 4;
            switch (type) {
                case 0x00:  //STS4x  温度传感器
                    break;
                case 0x01: // SHT41  温湿度传感器
                    tSensor = new SensorBean();
                    tSensor.setImageId(R.mipmap.temperature);
                    tSensor.setTypeId(R.string.sensor_temperature);
                    tSensor.setUnity("℃");
                    tSensor.setType(Constants.T_VALUE);
                    hSensor = new SensorBean();
                    hSensor.setImageId(R.mipmap.humidity);
                    hSensor.setTypeId(R.string.sensor_humidity);
                    hSensor.setUnity("%RH");
                    hSensor.setType(Constants.H_VALUE);


                    break;
                case 0x02:// BMP280 气压传感器
                    nfcA.transceive(FMUtil.hexToByte("AD03E001EC88"));
                    nfcA.transceive(FMUtil.hexToByte("AD030100ED18"));
                    byte[] bytes = nfcA.transceive(FMUtil.hexToByte("3E18"));
                    dev = new byte[bytes.length - 1];
                    System.arraycopy(bytes, 1, dev, 0, dev.length);
                    nfcA.transceive(FMUtil.hexToByte("AD04E002ECF500"));

                    pSensor = new SensorBean();
                    pSensor.setImageId(R.mipmap.pressure);
                    pSensor.setTypeId(R.string.sensor_pressure);
                    pSensor.setUnity("Pa");
                    pSensor.setType(Constants.P_VALUE);

                    break;
                case 0x03:// BH1730 光照传感器
                    bSensor = new SensorBean();
                    bSensor.setImageId(R.mipmap.brightness);
                    bSensor.setTypeId(R.string.sensor_brightness);
                    bSensor.setUnity("LUX");
                    bSensor.setType(Constants.B_VALUE);
                    break;
                case 0x04:// SGP41 气体传感器
                    int value = sensor[i + 1] & (byte) 0x0F;
                    switch (value) {
                        case 0: // SGP41
                            initGas(nfcA);
                            gSensor = new SensorBean();
                            gSensor.setImageId(R.mipmap.gas);
                            gSensor.setTypeId(R.string.sensor_gas_nox);
                            gSensor.setUnity("");
                            break;
                        case 1: // GS-Y7 co2
                            cSensor = new SensorBean();
                            cSensor.setImageId(R.mipmap.gas);
                            cSensor.setTypeId(R.string.sensor_co2);
                            cSensor.setUnity("");
                            cSensor.setType(Constants.CO2_VALUE);
                            break;
                    }

                    break;

                case 0x05:

                    break;
            }

        }
        Handler handler = new Handler();
        SensorBean finalTSensor = tSensor;
        SensorBean finalHSensor = hSensor;
        SensorBean finalPSensor = pSensor;
        SensorBean finalBSensor = bSensor;
        SensorBean finalGSensor = gSensor;
        SensorBean finalCSensor = cSensor;
        byte[] finalDev = dev;
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    ArrayList<SensorBean> list = new ArrayList<>();
                    for (int i = 0; i < sensor[0]; i++) {
                        int type = (sensor[i + 1] & (byte) 0xF0) >> 4;
                        byte[] result = null;
                        switch (type) {
                            case 0x00:  //STS4x  温度传感器
                                break;
                            case 0x01: // SHT41  温湿度传感器
                                result = nfcA.transceive(FMUtil.hexToByte("AD03E00188FD"));
                                LogUtil.d(FMUtil.byteToHex(result));
                                SystemClock.sleep(15);
                                break;
                            case 0x02:// BMP280 气压传感器
                                result = nfcA.transceive(FMUtil.hexToByte("AD04E002ECF429"));
                                LogUtil.d(FMUtil.byteToHex(result));
                                SystemClock.sleep(150);
                                break;
                            case 0x03:// BH1730 光照传感器
                                result = nfcA.transceive(FMUtil.hexToByte("AD04E002528702"));
                                LogUtil.d(FMUtil.byteToHex(result));
                                result = nfcA.transceive(FMUtil.hexToByte("AD04E0025281DA"));
                                LogUtil.d(FMUtil.byteToHex(result));
                                result = nfcA.transceive(FMUtil.hexToByte("AD04E00252800B"));
                                LogUtil.d(FMUtil.byteToHex(result));
                                result = nfcA.transceive(FMUtil.hexToByte("AD03E0015294"));
                                LogUtil.d(FMUtil.byteToHex(result));
                                SystemClock.sleep(300);
                                break;
                            case 0x04://  气体传感器
                                int value = sensor[i + 1] & (byte) 0x0F;
                                switch (value) {
                                    case 0: // SGP41
                                        result = nfcA.transceive(FMUtil.hexToByte("AD0AE008B226198000A2666693"));
                                        LogUtil.d(FMUtil.byteToHex(result));
                                        SystemClock.sleep(1);
                                        break;
                                    case 1: // GS-Y7 co2

                                        break;
                                }

                                break;

                            case 0x05:

                                break;
                        }
                    }
                    SystemClock.sleep(600);
                    for (int i = 0; i < sensor[0]; i++) {
                        int type = (sensor[i + 1] & (byte) 0xF0) >> 4;

                        switch (type) {
                            case 0x00:  //STS4x  温度传感器
                                break;
                            case 0x01: // SHT41  温湿度传感器
                                byte[] bytes = nfcA.transceive(FMUtil.hexToByte("AD0301008905"));
                                LogUtil.d(FMUtil.byteToHex(bytes));
                                byte[] FIFO_DATA = nfcA.transceive(FMUtil.hexToByte("3E05"));
                                LogUtil.d(FMUtil.byteToHex(FIFO_DATA));
                                double temperature = 175 * (SensorUtils.getUint8(FIFO_DATA[1]) * 256 + SensorUtils.getUint8(FIFO_DATA[2])) / 65535.0 - 45;
                                double humidity = 125 * (SensorUtils.getUint8(FIFO_DATA[4]) * 256 + SensorUtils.getUint8(FIFO_DATA[5])) / 65535.0 - 6;
                                LogUtil.d(temperature);
                                LogUtil.d(humidity);
                                if (finalTSensor != null) {
                                    finalTSensor.setValue(temperature);
                                    finalTSensor.setTime(System.currentTimeMillis());
                                    list.add(finalTSensor);
                                }
                                if (finalHSensor != null) {
                                    finalHSensor.setValue(humidity);
                                    finalHSensor.setTime(System.currentTimeMillis());
                                    list.add(finalHSensor);
                                }
                                break;
                            case 0x02:// BMP280 气压传感器

                                double[] doubles = parsePressure(nfcA, finalDev);
                                if (finalPSensor != null) {
                                    finalPSensor.setValue(doubles[1]);
                                    finalPSensor.setTime(System.currentTimeMillis());
                                    list.add(finalPSensor);
                                }

                                break;
                            case 0x03:// BH1730 光照传感器
                                double v = parseBrightnessData(nfcA);
                                if (finalBSensor != null) {
                                    finalBSensor.setValue(v);
                                    finalBSensor.setTime(System.currentTimeMillis());
                                    list.add(finalBSensor);
                                }
                                break;
                            case 0x04:// SGP41 气体传感器
                                int value = sensor[i + 1] & (byte) 0x0F;
                                switch (value) {
                                    case 0:
                                        parseGas(nfcA);
                                        if (finalGSensor != null) {
                                            finalGSensor.setValue(nox_index_value);
                                            finalGSensor.setType(Constants.NOX_VALUE);
                                            finalGSensor.setTime(System.currentTimeMillis());
                                            list.add(finalGSensor);
                                            SensorBean sensorBean = new SensorBean();
                                            sensorBean.setImageId(R.mipmap.gas);
                                            sensorBean.setTypeId(R.string.sensor_gas_voc);
                                            sensorBean.setUnity("");
                                            sensorBean.setValue(voc_index_value);
                                            sensorBean.setType(Constants.VOC_VALUE);
                                            sensorBean.setTime(System.currentTimeMillis());
                                            list.add(sensorBean);
                                        }
                                        break;
                                    case 1: // co2
                                        if (finalCSensor != null) {
                                            byte[] result = nfcA.transceive(FMUtil.hexToByte("AD0301008B03"));

                                            LogUtil.d(FMUtil.byteToHex(result));
                                            byte[] transceive = nfcA.transceive(FMUtil.hexToByte("3E03"));
                                            LogUtil.d("co2",FMUtil.byteToHex(transceive));
                                            finalCSensor.setValue(SensorUtils.byteArrayToUnsignedInt(new byte[]{transceive[1], transceive[2]}));
                                            finalCSensor.setTime(System.currentTimeMillis());
                                            finalCSensor.setType(Constants.CO2_VALUE);
                                            finalCSensor.setUnity("ppm");
                                            list.add(finalCSensor);
                                        }
                                        break;
                                }

                                break;
                            case 0x05:
                                break;
                        }


                    }
                    if (!list.isEmpty()) {
                        Bundle bundle = new Bundle();
                        bundle.putParcelableArrayList(Constants.KEY_HANDLE_SEND_MSG, list);
                        sendBundle(Constants.SUCCESS_RESULT, bundle);
                    } else {
                        sendMsg(Constants.NFC_NO_SENSOR);
                    }
                    handler.postDelayed(this, 1000);
                } catch (Exception e) {
                    e.printStackTrace();
                    sendMsg(Constants.NFC_UNKNOWN_ERROR);
                    handler.removeCallbacksAndMessages(null);
                    try {
                        nfcA.close();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }
            }
        };
        handler.postDelayed(runnable, 0);


    }


    public static void startSensor(NfcA nfcA, int type) throws IOException {

        byte[] reg = nfcA.transceive(FMUtil.hexToByte(Constants.COMMAND_WRITE_REG));
        LogUtil.d(FMUtil.byteToHex(reg));
        if (!"AAAA".equals(FMUtil.byteToHex(reg))) {
            sendMsg(Constants.NFC_UNKNOWN_ERROR);
            return;
        }
        byte[] sensor = nfcA.transceive(FMUtil.hexToByte(Constants.COMMAND_READ_SENSOR));
        LogUtil.d(FMUtil.byteToHex(sensor));
        byte[] FIFO_DATA = null;
        for (int i = 0; i < sensor[0]; i++) {
            int flag = (sensor[i + 1] & (byte) 0xF0) >> 4;
            byte[] result = null;
            switch (flag) {
                case 0x00:  //STS4x  温度传感器
                    break;
                case 0x01: // SHT41  温湿度传感器

                    break;
                case 0x02:// BMP280 气压传感器
                    result = nfcA.transceive(FMUtil.hexToByte("AD03E001EC88"));
                    LogUtil.d(FMUtil.byteToHex(result));
                    result = nfcA.transceive(FMUtil.hexToByte("AD030100ED18"));
                    LogUtil.d(FMUtil.byteToHex(result));
                    byte[] bytes = nfcA.transceive(FMUtil.hexToByte("3E18"));
                    LogUtil.d(FMUtil.byteToHex(bytes));
                    FIFO_DATA = new byte[bytes.length - 1];
                    System.arraycopy(bytes, 1, FIFO_DATA, 0, FIFO_DATA.length);
                    result = nfcA.transceive(FMUtil.hexToByte("AD04E002ECF500"));
                    LogUtil.d(FMUtil.byteToHex(result));
                    break;
                case 0x03:// BH1730 光照传感器
                    break;
                case 0x04:// SGP41 气体传感器


                    break;
            }

        }
        Handler handler = new Handler();
        byte[] finalFIFO_DATA = FIFO_DATA;
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    //                    switch (type) {
                    //                        case Constants.T_VALUE:
                    //                            readTemperature(nfcA);
                    //                            break;
                    //                        case Constants.H_VALUE:
                    //                            readHumidity(nfcA);
                    //                            break;
                    //                        case Constants.B_VALUE:
                    //                            readBrightness(nfcA);
                    //                            break;
                    //                        case Constants.P_VALUE:
                    //                            readPressure(nfcA, finalFIFO_DATA);
                    //                            break;
                    //                    }
                    parseSensorData(nfcA, sensor, type, finalFIFO_DATA);

                    handler.postDelayed(this::run, 1000);
                } catch (Exception e) {
                    e.printStackTrace();
                    handler.removeCallbacksAndMessages(null);
                    sendMsg(Constants.NFC_UNKNOWN_ERROR);
                    try {
                        nfcA.close();
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }

            }
        };

        handler.postDelayed(runnable, 1000);

    }

    public static int voc_index_value;
    public static int nox_index_value;
    public static short[] sraw_voc = new short[48];
    public static short[] sraw_nox = new short[48];
    public static GasIndexAlgorithmParams voc_params = new GasIndexAlgorithmParams();
    public static GasIndexAlgorithmParams nox_params = new GasIndexAlgorithmParams();

    private static void initGas(NfcA nfcA) throws IOException {
        byte[] EEPROM_DATA = nfcA.transceive(FMUtil.hexToByte("3006"));

        if (SensorUtils.getUint8(EEPROM_DATA[0]) != 0xFF)  // 0xFF代表已经完成
        {
            for (int i = 0; i < 10; i++) {
                // 开启加热
                nfcA.transceive(FMUtil.hexToByte("AD0AE008B226128000A2666693"));
                SystemClock.sleep(100);
                nfcA.transceive(FMUtil.hexToByte("AD030100B303"));
                nfcA.transceive(FMUtil.hexToByte("3E03"));
                SystemClock.sleep(800);


            }
            nfcA.transceive(FMUtil.hexToByte("A206FF000000"));
        }

        SensorUtils.GasIndexAlgorithm_init(voc_params, Constants.GasIndexAlgorithm_ALGORITHM_TYPE_VOC);
        SensorUtils.GasIndexAlgorithm_init(nox_params, Constants.GasIndexAlgorithm_ALGORITHM_TYPE_NOX);

        for (int j = 0; j < 6; j++)                    //Voc[15:8]为Block中Byte0，Voc[7:0]为Block中Byte1
        {
            //非接读EEPROM VOC数据：即j=0:'3010',j=1:'3018',j=2:'3020',j=3:'3028'  每条命令16Bytes数据；
            EEPROM_DATA = nfcA.transceive(new byte[]{0x30, (byte) (0x10 + j * 8)});
            LogUtil.d("Gas-VOC->send", FMUtil.byteToHex(new byte[]{0x30, (byte) (0x10 + j * 8)}));
            LogUtil.d("Gas-VOC-rev", FMUtil.byteToHex(EEPROM_DATA));

            for (int i = 0; i < 8; i++) {
                int a = SensorUtils.getUint8(EEPROM_DATA[i * 2]) << 8;
                int b = SensorUtils.getUint8(EEPROM_DATA[i * 2 + 1]);
                int c = a + b;
                sraw_voc[i + j * 8] = (short) c;
            }

            //非接读EEPROM：NOX数据,即'3014','301C','3024','302C'
            EEPROM_DATA = nfcA.transceive(new byte[]{0x30, (byte) (0x14 + j * 8)});
            LogUtil.d("Gas-NOX->send", FMUtil.byteToHex(new byte[]{0x30, (byte) (0x14 + j * 8)}));
            LogUtil.d("Gas-NOX-rev", FMUtil.byteToHex(EEPROM_DATA));


            for (int i = 0; i < 8; i++) {
                int a = SensorUtils.getUint8(EEPROM_DATA[i * 2]) << 8;
                int b = SensorUtils.getUint8(EEPROM_DATA[i * 2 + 1]);
                int c = a + b;
                sraw_nox[i + j * 8] = (short) c;
            }
        }
        for (int i = 0; i < 45; i++) {
            voc_index_value = SensorUtils.GasIndexAlgorithm_process(voc_params, sraw_voc[i]);
            nox_index_value = SensorUtils.GasIndexAlgorithm_process(nox_params, sraw_nox[i]);
        }
    }


    public static void parseGas(NfcA nfcA) throws IOException {
        nfcA.transceive(FMUtil.hexToByte("AD030100B305"));
        byte[] bytes = nfcA.transceive(FMUtil.hexToByte("3E05"));
        LogUtil.d("Gas->3E05", FMUtil.byteToHex(bytes));
        byte[] FIFO_DATA = new byte[bytes.length - 1];
        System.arraycopy(bytes, 1, FIFO_DATA, 0, FIFO_DATA.length);
        sraw_voc[0] = SensorUtils.getUint8(FIFO_DATA[0]);
        sraw_voc[0] <<= 8;
        sraw_voc[0] |= SensorUtils.getUint8(FIFO_DATA[1]);
        sraw_nox[0] = SensorUtils.getUint8(FIFO_DATA[3]);
        sraw_nox[0] <<= 8;
        sraw_nox[0] |= SensorUtils.getUint8(FIFO_DATA[4]);
        voc_index_value = SensorUtils.GasIndexAlgorithm_process(voc_params, sraw_voc[0]);
        nox_index_value = SensorUtils.GasIndexAlgorithm_process(nox_params, sraw_nox[0]);
        SystemClock.sleep(1);
        LogUtil.d("voc", voc_index_value);
        LogUtil.d("nox", nox_index_value);
    }


    public static void parseSensorData(NfcA nfcA, byte[] sensor, int type, byte[] dev) throws IOException {

        for (int i = 0; i < sensor[0]; i++) {
            int flag = (sensor[i + 1] & (byte) 0xF0) >> 4;
            switch (flag) {
                case 0x00:  //STS4x  温度传感器
                    break;
                case 0x01: // SHT41  温湿度传感器
                    readTemperature(nfcA);
                    readHumidity(nfcA);
                    break;
                case 0x02:// BMP280 气压传感器
                    readPressure(nfcA, dev);
                    break;
                case 0x03:// BH1730 光照传感器
                    readBrightness(nfcA);
                    break;
                case 0x04:// SGP41 气体传感器
                    break;
            }
        }
    }

    public static void readTemperature(NfcA nfcA) throws IOException {
        byte[] result = nfcA.transceive(FMUtil.hexToByte("AD03E00188FD"));
        LogUtil.d(FMUtil.byteToHex(result));
        SystemClock.sleep(20);
        byte[] bytes = nfcA.transceive(FMUtil.hexToByte("AD0301008905"));
        LogUtil.d(FMUtil.byteToHex(bytes));
        byte[] FIFO_DATA = nfcA.transceive(FMUtil.hexToByte("3E05"));
        LogUtil.d(FMUtil.byteToHex(FIFO_DATA));
        double temperature = 175 * (SensorUtils.getUint8(FIFO_DATA[1]) * 256 + SensorUtils.getUint8(FIFO_DATA[2])) / 65535.0 - 45;
        LogUtil.d(temperature);
        if (calcCRC(new byte[]{FIFO_DATA[1], FIFO_DATA[2]}) == FIFO_DATA[3]) {
            sendMsg(Constants.T_VALUE, String.format("%.2f", temperature));
        }

    }

    public static void readHumidity(NfcA nfcA) throws IOException {
        byte[] result = nfcA.transceive(FMUtil.hexToByte("AD03E00188FD"));
        LogUtil.d(FMUtil.byteToHex(result));
        SystemClock.sleep(20);
        byte[] bytes = nfcA.transceive(FMUtil.hexToByte("AD0301008905"));
        LogUtil.d(FMUtil.byteToHex(bytes));
        byte[] FIFO_DATA = nfcA.transceive(FMUtil.hexToByte("3E05"));
        LogUtil.d("湿度原始值", FMUtil.byteToHex(FIFO_DATA));
        double humidity = 125 * (SensorUtils.getUint8(FIFO_DATA[4]) * 256 + SensorUtils.getUint8(FIFO_DATA[5])) / 65535.0 - 6;
        LogUtil.d(humidity);
        if (calcCRC(new byte[]{FIFO_DATA[4], FIFO_DATA[5]}) == FIFO_DATA[6]) {
            sendMsg(Constants.H_VALUE, String.format("%.2f", humidity));
        }

    }

    /**
     * BMP280 读取温度和压力结果
     *
     * @param nfcA
     * @throws IOException
     */
    public static void readPressure(NfcA nfcA, byte[] dev) throws IOException {
        byte[] result = nfcA.transceive(FMUtil.hexToByte("AD04E002ECF429"));
        LogUtil.d(FMUtil.byteToHex(result));
        SystemClock.sleep(150);
        parsePressure(nfcA, dev);
        //sendMsg(Constants.P_VALUE, String.format("%.0f", doubles[1]));

    }

    public static double[] parsePressure(NfcA nfcA, byte[] dev) throws IOException {
        byte[] result = null;
        result = nfcA.transceive(FMUtil.hexToByte("AD03E001ECF7"));
        result = nfcA.transceive(FMUtil.hexToByte("AD030100ED05"));
        result = nfcA.transceive(FMUtil.hexToByte("3E05"));
        LogUtil.d("原始值", FMUtil.byteToHex(result));
        byte[] FIFO_DATA = new byte[result.length - 1];
        System.arraycopy(result, 1, FIFO_DATA, 0, FIFO_DATA.length);
        LogUtil.d("dev", FMUtil.byteToHex(dev));

        //        double[] doubles = SensorUtils.parse_sensor_data(FMUtil.hexToByte("47D800821F00"), FMUtil.hexToByte("426C4B663200919761D6D00B111EECFEF9FF8C3CF8C6701700"));
        double[] doubles = SensorUtils.parse_sensor_data(FIFO_DATA, dev);
        sendMsg(Constants.P_VALUE, String.format("%.0f", doubles[1]));
        return doubles;
    }

    public static void readBrightness(NfcA nfcA) throws IOException {
        byte[] result = nfcA.transceive(FMUtil.hexToByte("AD04E002528702"));
        LogUtil.d(FMUtil.byteToHex(result));
        result = nfcA.transceive(FMUtil.hexToByte("AD04E0025281DA"));
        LogUtil.d(FMUtil.byteToHex(result));
        result = nfcA.transceive(FMUtil.hexToByte("AD04E00252800B"));
        LogUtil.d(FMUtil.byteToHex(result));
        result = nfcA.transceive(FMUtil.hexToByte("AD03E0015294"));
        LogUtil.d(FMUtil.byteToHex(result));
        SystemClock.sleep(300);
        double v = parseBrightnessData(nfcA);
        sendMsg(Constants.B_VALUE, String.format("%.2f", v));


    }

    private static double parseBrightnessData(NfcA nfcA) throws IOException {
        nfcA.transceive(FMUtil.hexToByte("AD0301005303"));
        byte[] FIFO_DATA = nfcA.transceive(FMUtil.hexToByte("3E03"));
        LogUtil.d("光照原始值", FMUtil.byteToHex(FIFO_DATA));
        //        FIFO_DATA = FMUtil.hexToByte("03E0A42415");
        int DATA0 = SensorUtils.getUint8(FIFO_DATA[2]) * 256 + SensorUtils.getUint8(FIFO_DATA[1]);
        int DATA1 = SensorUtils.getUint8(FIFO_DATA[4]) * 256 + SensorUtils.getUint8(FIFO_DATA[3]);
        double Lx;
        if (DATA1 / DATA0 < 0.26) {
            Lx = (1.290 * DATA0 - 2.733 * DATA1) / Constants.GAIN * 102.6 / Constants.ITIME_MS;

        } else if (DATA1 / DATA0 < 0.55) {
            Lx = (0.795 * DATA0 - 0.859 * DATA1) / Constants.GAIN * 102.6 / Constants.ITIME_MS;

        } else if (DATA1 / DATA0 < 1.09) {
            Lx = (0.510 * DATA0 - 0.345 * DATA1) / Constants.GAIN * 102.6 / Constants.ITIME_MS;

        } else if (DATA1 / DATA0 < 2.13) {
            Lx = (0.276 * DATA0 - 0.130 * DATA1) / Constants.GAIN * 102.6 / Constants.ITIME_MS;

        } else {
            Lx = 0;

        }
        LogUtil.d(Lx);
        return Lx;
    }


    public static byte calcCRC(byte[] data) {
        byte crc = (byte) 0xff;
        byte[] temp = new byte[2];
        temp[0] = (byte) (data[0] & 0xFF);
        temp[1] = (byte) (data[1] & 0xFF);
        for (int i = 0; i < 2; i++) {
            crc ^= temp[i];
            for (int j = 8; j > 0; --j) {
                if ((byte) (crc & 0x80) != 0) {
                    crc = (byte) ((crc << 1) ^ 0x31);
                } else {
                    crc <<= 1;
                }
            }
        }
        LogUtil.d("crc", crc);
        return crc;

    }

    public static void sendMsg(int what) {
        sendMsg(what, null);
    }

    public static void sendMsg(int what, String msg) {
        Message message = new Message();
        message.what = what;
        Bundle bundle = new Bundle();
        bundle.putString(Constants.KEY_HANDLE_SEND_MSG, msg);
        message.setData(bundle);
        BaseHandler.getInstance().sendMessage(message);
    }

    public static void sendBundle(int what, Bundle bundle) {
        Message message = new Message();
        message.what = what;
        message.setData(bundle);
        BaseHandler.getInstance().sendMessage(message);
    }

}
